/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.sys.service.impl;

import com.easy.common.util.Utility;
import com.easy.common.utils.DateUtils;
import com.easy.core.persistence.Page;
import com.easy.core.service.impl.BaseServiceImpl;
import com.easy.modules.sys.entity.Log;
import com.easy.modules.sys.mapper.LogMapper;
import com.easy.modules.sys.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 日志Service
 * @author easy
 * @version 2017-05-16
 */
@Service
@Transactional(readOnly = true)
public class LogServiceImpl  extends BaseServiceImpl implements LogService {

	@Autowired
	private LogMapper mapper;

	
	/**
	 * 删除全部数据
	 */
	@Transactional(readOnly = false)
	public void empty(){
		
		mapper.empty();
	}

	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	public Log get(String id) {
		return mapper.get(id);
	}

	/**
	 * 获取单条数据
	 * @param logId
	 * @return
	 */
	public Log getByLogId(Integer logId) {
		return mapper.getByLogId(logId);
	}

	/**
	 * 获取单条数据
	 * @param log
	 * @return
	 */
	public Log get(Log log) {
		return mapper.get(log);
	}

	/**
	 * 查询列表数据
	 * @param log
	 * @return
	 */
	public List<Log> findList(Log log) {
		dataRuleFilter(log);
		return mapper.findList(log);
	}

	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param log
	 * @return
	 */
	public Page<Log> findPage(Page<Log> page, Log log) {
		// 设置默认时间范围，默认当前月
		if (log.getBeginTime() == null){
			log.setBeginTime(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
		}
		if (log.getEndTime() == null){
			log.setEndTime(DateUtils.addMonths(log.getBeginTime(), 1));
		}

		dataRuleFilter(log);
		log.setPage(page);
		page.setList(mapper.findList(log));
		return page;
	}

	/**
	 * 保存数据（插入或更新）
	 * @param log
	 */
	@Transactional(readOnly = false)
	public void save(Log log) {
		if (Utility.isNullorEmpty(log.getId()) || log.getLogId()<1){
			log.preInsert();
			mapper.insert(log);
		}else{
			log.preUpdate();
			mapper.update(log);
		}
	}

	/**
	 * 删除数据
	 * @param log
	 */
	@Transactional(readOnly = false)
	public void delete(Log log) {
		mapper.delete(log);
	}


	/**
	 * 删除全部数据
	 * @param logs
	 */
	@Transactional(readOnly = false)
	public void deleteAll(Collection<Log> logs) {
		for (Log log : logs) {
			mapper.delete(log);
		}
	}

	/**
	 * 删除全部数据
	 * @param logs
	 */
	@Transactional(readOnly = false)
	public void deleteAllByLogic(Collection<Log> logs) {
		for (Log log : logs) {
			mapper.deleteLogic(log);
		}
	}


	/**
	 * 获取单条数据
	 * @param propertyName, value
	 * @return
	 */
	public Log findUniqueByProperty(String propertyName, Object value){
		return mapper.findUniqueByProperty(propertyName, value);
	}

	/**
	 * 动态sql
	 * @param sql
	 */

	public List<Object> executeSelectSql(String sql){
		return mapper.execSelectSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeInsertSql(String sql){
		mapper.execInsertSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeUpdateSql(String sql){
		mapper.execUpdateSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeDeleteSql(String sql){
		mapper.execDeleteSql(sql);
	}

}
